<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 商品属性
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-04-29 17:43:42 +0800 (二, 2014-04-29) $
 * @version : $Id: attr.php 87 2014-04-29 09:43:42Z spike $
 * @rev     : $Revision: 87 $
 */
class Attr extends CI_Controller 
{  
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * [浏览]
     */
    public function browse_attr_group()
    {
        $params = $this->input->get();
        if($params)
        {
            $this->load->model('Attr_model');

            $list = $this->Attr_model->browse_attr_group($params, $params['page'], $params['size']);

            $this->load->library('pagination');
            $config['total_rows'] = $list['total'];
            $config['per_page']   = $list['size'];
            $config['suffix']     = '#data-table';

            $this->pagination->initialize($config); 
            $pagination = $this->pagination->create_links();
        }

        // 商品筛选数据
        $attr_group_status = $this->config->item('attr_group_status');

        // 统计
        $this->load->dao('Attr_group_dao');
        $total = $this->Attr_group_dao->count(array(
            'is_delete' => UNDELETED
        ));

        $data = array(
            'default'           => $params ? $params : array(),
            'total'             => $total,
            'list'              => $list,
            'pagesize'          => $this->config->item('pagesize'),
            'attr_group_status' => $attr_group_status,
            'pagination'        => $pagination,
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('attr/browse_attr_group.tpl');
        $this->load->view('home/footer.tpl');
    }

    // --------------------------------------------------------------------

    /**
     * [新增]
     */
    public function add_attr_group()
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp('add_attr_group', $params);

            $this->load->model('Attr_model');

            if( FALSE === $id = $this->Attr_model->add_attr_group($params) )
            {
                show_error($this->Attr_model->get_error(), 200, '错误', 'error_general_lite');
            }

            temp('add_attr_group', NULL);

            show_message(
                "新增属性模版成功！", 
                array('继续添加属性' => DOMAIN."attr/edit_attr_group/{$id}"),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Attr_model');

            // 整理暂存数据
            $temp = temp('add_attr_group');

            $page_style    = $this->config->item('page_style');
            $data = array(
                'temp'           => $temp,
                'page_style'     => $page_style['LITE'],
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('attr/add_attr_group.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [新增属性]
     */
    public function add_attr()
    {
        $is_ajax = $this->input->get('is_ajax');

        $attr_group_id = $this->input->get('id') ? $this->input->get('id') : NULL;
        $name          = $this->input->get('name');

        $this->load->model('Attr_model');
        if( FALSE === $attr_id = $this->Attr_model->add_attr($attr_group_id, array('name'=>$name)))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Attr_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE, $attr_id);
            }
        }
    }

    /**
     * [新增属性值]
     */
    public function add_attr_value()
    {
        $is_ajax = $this->input->get('is_ajax');
        $attr_id = $this->input->get('id') ? $this->input->get('id') : NULL;
        $name    = $this->input->get('name');

        $this->load->model('Attr_model');
        if( FALSE === $attr_value_id = $this->Attr_model->add_attr_value($attr_id, array('name'=>$name)))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Attr_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE, $attr_id);
            }
        }
    }

    // --------------------------------------------------------------------

    /**
     * [查看]
     */
    public function view_attr_group($id = NULL)
    {
        $this->load->model('Attr_model');
        if( ! $attr_group = $this->Attr_model->get_attr_group($id, FALSE, TRUE))
        {
            show_error($this->Attr_model->get_error(), 200, '错误', 'error_general_lite');
        }

        // 绑定操作人数据
        $this->load->model('Rbac_model');
        $this->Rbac_model->wrap_admin_info($attr_group, TRUE, 'create_admin');
        $this->Rbac_model->wrap_admin_info($attr_group, TRUE, 'update_admin');

        $page_style = $this->config->item('page_style');
        $data = array(
            'attr_group'        => $attr_group,
            'attr_group_status' => $this->config->item('attr_group_status'),
            'page_style'        => $page_style['LITE'],
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('attr/view_attr_group.tpl');
        $this->load->view('home/footer.tpl');
    }

    // --------------------------------------------------------------------

    /**
     * [编辑]
     */
    public function edit_attr_group($id = NULL)
    {
        $this->load->model('Attr_model');
        if( ! $attr_group = $this->Attr_model->get_attr_group($id, FALSE, TRUE))
        {
            show_error($this->Attr_model->get_error(), 200, '错误', 'error_general_lite');
        }

         // 绑定操作人数据
        $this->load->model('Rbac_model');
        $this->Rbac_model->wrap_admin_info($attr_group, TRUE, 'create_admin');
        $this->Rbac_model->wrap_admin_info($attr_group, TRUE, 'update_admin');
        
        $page_style     = $this->config->item('page_style');
        $data = array(
            'attr_group'        => $attr_group,
            'page_style'        => $page_style['LITE'],
            'attr_group_status' => $this->config->item('attr_group_status'),
            'css'               => array(JS_DOMAIN.'jquery/jquery-nestable/jquery.nestable.css'),
            'js'                => array('jquery/jquery-nestable/jquery.nestable.js','jquery/jquery-json/jquery.json.js'),
        );
        $this->load->view('home/header.tpl', $data);
        $this->load->view('attr/edit_attr_group.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [编辑属性值]
     */
    public function edit_attr_value($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $params  = $this->input->post();
            $is_ajax = $this->input->post('is_ajax');

            $this->load->model('Attr_model');

            if( FALSE === $this->Attr_model->edit_attr_value($id, $params) )
            {
                if($is_ajax === '1')
                {
                    json_exit($this->Attr_model->get_error());
                }
            }
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
        else
        {
            $this->load->model('Attr_model');
            if( ! $attr = $this->Attr_model->get_attr($id, FALSE, TRUE))
            {
                show_error($this->Attr_model->get_error(), 200, '错误', 'error_general_lite');
            }
            $page_style     = $this->config->item('page_style');
            $data = array(
                'attr'       => $attr,
                'page_style' => $page_style['LITE'],
                'css'        => array(JS_DOMAIN.'jquery/jquery-nestable/jquery.nestable.css'),
                'js'         => array('jquery/jquery-nestable/jquery.nestable.js','jquery/jquery-json/jquery.json.js'),

            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('attr/edit_attr_value.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [编辑属性值]
     */
    public function edit_attr($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $params  = $this->input->post();
            $is_ajax = $this->input->post('is_ajax');

            $this->load->model('Attr_model');

            if( FALSE === $this->Attr_model->edit_attr($id, $params) )
            {
                if($is_ajax === '1')
                {
                    json_exit($this->Attr_model->get_error());
                }
            }
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }

    /**
     * [批量编辑属性排序]
     */
    public function edit_attr_sort_batch()
    {
        $data    = $this->input->get('data');
        $is_ajax = $this->input->get('is_ajax');
        $data    = json_decode($data, TRUE);
        
        $this->load->model('Attr_model');
        if( FALSE === $this->Attr_model->edit_attr_sort_batch($data))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Attr_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }

    /**
     * [批量编辑属性值排序]
     */
    public function edit_attr_value_sort_batch()
    {
        $data    = $this->input->get('data');
        $is_ajax = $this->input->get('is_ajax');
        $data    = json_decode($data, TRUE);
        
        $this->load->model('Attr_model');
        if( FALSE === $this->Attr_model->edit_attr_value_sort_batch($data))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Attr_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }

    // --------------------------------------------------------------------

    /**
     * [改变属性状态]
     */
    public function change_attr_status()
    {
        $id      = $this->input->get('id');
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Attr_model');
        if( FALSE === $this->Attr_model->change_attr_status($id, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Attr_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }

    /**
     * [改变属性状态]
     */
    public function change_attr_value_status()
    {
        $id      = $this->input->get('id');
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Attr_model');
        if( FALSE === $this->Attr_model->change_attr_value_status($id, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Attr_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }


    /**
     * [改变属性模版状态]
     */
    public function change_attr_group_status($id = NULL)
    {
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Attr_model');
        if( FALSE === $this->Attr_model->change_attr_group_status($id, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Attr_model->get_error());
            }
            else
            {
                show_error($this->Attr_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
            else
            {
                show_message("改变属性模版状态成功！", array(
                    '返回商品属性' => DOMAIN."attr/browse_attr_group",
                ));
            }
        }
    }

    // --------------------------------------------------------------------
   
    /**
     * [删除属性]
     */
    public function del_attr()
    {
        $id      = $this->input->get('id');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Attr_model');
        if( FALSE === $this->Attr_model->del_attr($id))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Attr_model->get_error());
            }
            else
            {
                show_error($this->Attr_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }

    /**
     * [删除属性值]
     */
    public function del_attr_value()
    {
        $id      = $this->input->get('id');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Attr_model');
        if( FALSE === $this->Attr_model->del_attr_value($id))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Attr_model->get_error());
            }
            else
            {
                show_error($this->Attr_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }
}
/* End of file */